<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>欢迎登录</h2>
        <p>在线教育平台</p>
      </div>
      <el-form :model="form" class="login-form" ref="formRef">
        <el-form-item>
          <el-input 
            v-model="form.username" 
            placeholder="用户名"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item>
          <el-input 
            v-model="form.password" 
            type="password" 
            placeholder="密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin" :loading="loading">登录</el-button>
        </el-form-item>
        <el-alert v-if="error" :title="error" type="error" show-icon style="margin-top:10px;" />
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const store = useStore()
const router = useRouter()
const form = ref({ username: '', password: '' })
const error = ref('')
const formRef = ref(null)
const loading = ref(false)

const handleLogin = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    loading.value = true
    
    // 模拟登录验证
    if (form.value.username === 'admin' && form.value.password === 'admin123456') {
      // 管理员登录
      const userInfo = {
        username: 'admin',
        email: 'admin@example.com',
        role: 'admin',
        avatar: ''
      }
      // 清除旧的用户信息
      localStorage.clear()
      // 保存新的用户信息
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
      localStorage.setItem('isLoggedIn', 'true')
      localStorage.setItem('isAdmin', 'true')
      localStorage.setItem('avatar', userInfo.avatar)

      ElMessage.success('登录成功')
      console.log('Attempting router push for admin')
      router.push('/admin/profile')
      console.log('Admin login successful, attempting to push to /admin/profile')
    } else if (form.value.username === 'ptyh' && form.value.password === '123456') {
      // 普通用户登录
      const userInfo = {
        username: 'ptyh',
        email: '333333@qq.com',
        role: 'user',
        avatar: ''
      }
      // 清除旧的用户信息
      localStorage.clear()
      // 保存新的用户信息
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
      localStorage.setItem('isLoggedIn', 'true')
      localStorage.setItem('isAdmin', 'false')
      ElMessage.success('登录成功')
      router.push('/user/profile')
    } else {
      ElMessage.error('用户名或密码错误')
    }
  } catch (error) {
    console.error('Login error:', error)
    error.value = error.message || '登录失败，请检查用户名和密码。'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-box {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  animation: slideUp 0.5s ease-out;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #303133;
  margin-bottom: 10px;
}

.login-header p {
  color: #909399;
  font-size: 14px;
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style> 